import pygame
from constants import Constants


class Resources:
    """加载和管理游戏资源的类"""

    def __init__(self):
        self.background_main = None
        self.background_game = None
        self.avatars = []
        self.font = None
        self.font_small = None

        self.load_resources()

    def load_resources(self):
        # 加载背景图片
        try:
            self.background_main = pygame.image.load("../png/001.png")
            self.background_main = pygame.transform.scale(self.background_main, (Constants.WIDTH, Constants.HEIGHT))

            self.background_game = pygame.image.load("../png/002.png")
            self.background_game = pygame.transform.scale(self.background_game, (Constants.WIDTH, Constants.HEIGHT))

        except pygame.error as e:
                print(f"图片加载失败: {e}")
                self.background_main = pygame.Surface((Constants.WIDTH, Constants.HEIGHT))
                self.background_main.fill((255, 255, 255))  # 白色背景备用
                self.background_game = pygame.Surface((Constants.WIDTH, Constants.HEIGHT))
                self.background_game.fill((200, 200, 200))  # 灰色背景备用

        # 加载头像图片
        for i in range(3, 7):
            try:
                avatar = pygame.image.load(f"../png/00{i}.png")
                avatar = pygame.transform.scale(avatar, Constants.AVATAR_SIZE)
                self.avatars.append(avatar)
            except pygame.error as e:
                print(f"头像图片加载失败: {e}")
                avatar = pygame.Surface(Constants.AVATAR_SIZE)
                avatar.fill((255, 0, 0))  # 红色占位符
                self.avatars.append(avatar)

        # 加载字体
        try:
            self.font = pygame.font.Font("C:/Windows/Fonts/simhei.ttf", Constants.FONT_SIZE)
            self.font_small = pygame.font.Font("C:/Windows/Fonts/simhei.ttf", Constants.SMALL_FONT_SIZE)
        except FileNotFoundError:
            print("未找到字体文件，请检查路径是否正确！")
            self.font = pygame.font.Font(None, Constants.FONT_SIZE)
            self.font_small = pygame.font.Font(None, Constants.SMALL_FONT_SIZE)